package com.blackfish.bag;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.multipart.FileRenamePolicy;

import com.blackfish.bag.BagService;
import com.blackfish.common.model.Bag;
import com.jfinal.aop.Before;

@Before(BagInterceptor.class)
public class BagController extends Controller{
	static BagService service = new BagService();
	
    public final class Preference {  
        public static final String _PATH = "img/test/";  
    }  
	
	public void index() {
		Object value = service.paginate(1,10); //需要修改
		setAttr("bagPage", value);
		//setAttr("bagPageM", value);  menu的包
		render("index.html");
	}

	public void table(){
		List<Bag> bags = service.findAll();
		renderJson(bags);
	}	
	
	public void delete(){
		String[] idStrings = this.getParaValues("ids[]");
		for(String idString : idStrings){
			int id=  Integer.valueOf(idString).intValue();
			if (service.deleteById(id)==false){
				renderText("error");
				return;
			}
		}
		renderText("ok");
	}
	
	public void edit(){
		HttpServletRequest request = getRequest();  
        String basePath = request.getContextPath();   
        //存储路径  
        String path = getSession().getServletContext().getRealPath(Preference._PATH);  
        long fileLength = 0;
        UploadFile file= getFile("file"); 
        fileLength = file.getFile().length();            
        String bagName = getPara("name");
        int id = Integer.valueOf(getPara("id")).intValue();
        String oldFileName = getPara("oldname");
        String fileName = "";  
        
        if( fileLength > 200*1024*1024) {  
            System.err.println("文件长度超过限制，必须小于200M");  
            renderText("error"); 
        }else{  
            //上传文件  
            String type = file.getFileName().substring(file.getFileName().lastIndexOf(".")); // 获取文件的后缀  
            fileName = bagName + type; // 对文件重命名取得的文件名+后缀  
            String dest = path + "/" + fileName;  
            file.getFile().renameTo(new File(dest));  
            String realFile = basePath + "./" + Preference._PATH +  fileName;          
            String fname="/"+fileName;  
            if(service.editById(id,bagName,realFile)){
            	renderText("ok");
            }else{
            	renderText("error");
            }
        }  
	}
	
	public void editName(){
		UploadFile file= getFile("file"); 
        String bagName = getPara("name");
        //int id = getParaToInt("id");
        String idString = getPara("id");
        int id = Integer.valueOf(idString).intValue();
        String oldFileName = getPara("oldfile");
        if(service.editById(id,bagName,oldFileName)){
            	renderText("ok");
           }else{
            	renderText("error");
         }
	}
	
	public void add(){
		HttpServletRequest request = getRequest();  
        String basePath = request.getContextPath();   
        //存储路径  
        String path = getSession().getServletContext().getRealPath(Preference._PATH);  
        UploadFile file = getFile("file"); 
        String bagName = getPara("name");
        String fileName = "";  
        long fileLength = file.getFile().length();
        if( fileLength > 200*1024*1024) {  
            System.err.println("文件长度超过限制，必须小于200M");  
            renderText("error"); 
        }else{  
            //上传文件  
            String type = file.getFileName().substring(file.getFileName().lastIndexOf(".")); // 获取文件的后缀  
            fileName = bagName + type; // 对文件重命名取得的文件名+后缀  
            String dest = path + "/" + fileName;  
            file.getFile().renameTo(new File(dest));  
            String realFile = basePath + "./" + Preference._PATH +  fileName;          
            String fname="/"+fileName;  
            //setAttr("fname", fname);  
            //setAttr("url", realFile);  
            if(service.saveBag(bagName,realFile)){
            	renderText("ok");
            }else{
            	renderText("error");
            }
        }          
	}
}
